﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ELearning.Wpf.UI
{
    using ELearning.Data;
    using ELearning.Data.LearningDatasetTableAdapters;

    /// <summary>
    /// Interaction logic for wndSubjectView.xaml
    /// </summary>
    public partial class wndSubjectView : Window
    {
        private readonly long _subjectId;

        private List<TestDisplay> _tests;

        public wndSubjectView(long subjectId, string subjectName)
        {
            _subjectId = subjectId;
            InitializeComponent();
            lbSubjectName.Content = subjectName;
            LoadData(subjectId);
        }

        private void LoadData(long subjectId)
        {
            using (TestsTableAdapter tableAdapter = new TestsTableAdapter())
            {
                var tests = tableAdapter.GetDataBySubjectID(subjectId);
                if (tests == null || tests.Count <= 0)
                {
                    return;
                }

                _tests = new List<TestDisplay>(tests.Count);
                foreach (var test in tests)
                {
                    _tests.Add(new TestDisplay { Id = test.ID, Name = test.Name });
                }

                lbTests.DataContext = null;
                lbTests.DataContext = _tests;
            }
        }

        private void btAddTest_Click(object sender, RoutedEventArgs e)
        {
            wndCreateTest wndCreate = new wndCreateTest(_subjectId);
            wndCreate.ShowDialog();
        }

        private void lbTests_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lbTests.SelectedItem == null)
            {
                return;
            }

            var id = ((TestDisplay)lbTests.SelectedItem).Id;
            wndTestViewer wndTestViewer = new wndTestViewer(id);
            wndTestViewer.ShowDialog();
        }

        private void btEditTest_Click(object sender, RoutedEventArgs e)
        {
            if (lbTests.SelectedItem == null)
            {
                return;
            }

            var id = ((TestDisplay)lbTests.SelectedItem).Id;
            wndCreateTest wndCreateTest = new wndCreateTest(_subjectId, id);
            wndCreateTest.ShowDialog();
        }
    }
}
